package com.wayhome.srpingbootmybatis.placeCloud.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.sun.javafx.binding.StringFormatter;
import com.wayhome.srpingbootmybatis.mbs.expertimport.dto.ExpertTemplate;
import com.wayhome.srpingbootmybatis.placeCloud.dto.ImportPlaceData;
import com.wayhome.srpingbootmybatis.utils.HttpUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ImportPlaceController {

    private static String split = "','";

    private static String end = "');";

    public static void main(String[] args) throws Exception {

        ImportPlaceListener importPlaceListener = new ImportPlaceListener();

        File file = new File("/Users/liangcai/Desktop/place-cloud/shiyaoAndlerentang/ShenWei.xlsx");
        InputStream inputStream = new FileInputStream(file);
        EasyExcel.read(inputStream, ImportPlaceData.class, importPlaceListener).sheet().doRead();

        List<ImportPlaceData> list = importPlaceListener.getList();
//        List<String> jianlianList =  jianlian(list);
//        List<String> yaodianList = yaodian(list);
//        List<String> baseInfoList = baseInfo(list);
//        invokeSendPlugIn(list);
        yaodianBieMing(list);
    }


    private static List<String> jianlian(List<ImportPlaceData> list) {
        List<String> jianlianList = new ArrayList<>();
        System.out.println("#-------------------------间联信息表------------------------");
        for (ImportPlaceData importPlaceData : list) {
            String insert_phar_merchant = "insert into hibiz_db.place_cloud_phar_merchant(FIXMEDINS_CODE,HOSP_TYPE,RID," +
                    "COU_CHANNEL_CODE,COU_APP_ID) value ('" +
                    importPlaceData.getFixmedinsCode() + split +
                    importPlaceData.getHospType() + split +
                    importPlaceData.getFixmedinsCode() + split +
                    importPlaceData.getCouChannelCode() + split +
                    importPlaceData.getCouAppId()
                    + end;
            System.out.println(insert_phar_merchant);
            jianlianList.add(insert_phar_merchant);
        }
        System.out.println("#-------------------------间联信息表------------------------");
        return jianlianList;
    }

    private static List<String>  yaodian(List<ImportPlaceData> list) {
        System.out.println("#-----------------------签约信息表---------------------------");
        List<String> yaodianList = new ArrayList<>();
        for (ImportPlaceData importPlaceData : list) {
                String insert_phar = "insert into hibiz_db.place_cloud_phar(FIXMEDINS_CODE,RID,VALI_FLAG) value ('" +
                    importPlaceData.getFixmedinsCode() + split +
                    importPlaceData.getFixmedinsCode() + split +
                    importPlaceData.getValiFlag() + end;
            System.out.println(insert_phar);
            yaodianList.add(insert_phar);
        }
        System.out.println("#-----------------------签约信息表---------------------------");
        return yaodianList;
    }

    private static List<String>  baseInfo(List<ImportPlaceData> list) {
        System.out.println("#-----------------------云药店基本信息表---------------------------");
        List<String> beasInfoList = new ArrayList<>();


        for (ImportPlaceData data : list) {
            StringBuilder stringBuffer = new StringBuilder();
            stringBuffer.append("insert into hibiz_db.place_cloud_rtal_phac_b")
                    .append("(FIXMEDINS_CODE,") //机构编码
                    .append("PLACE_CLOUD_PHAC_NAME,") //机构名称
//                    .append("BEGNTIME,") //营业开始时间
//                    .append("ENDTIME,") //营业结束时间
                    .append("LNT,")//经度
                    .append("LAT,")//纬度
                    .append("ADDR,")//地址
//                    .append("IS_DIST,")//是否支持配送
//                    .append("DIST_RANGE,")//支持配送公里数
                    .append("VALI_FLAG,")//有效标志
                    .append("CRTER_ID,")//创建人
                    .append("CRTER_NAME,")//创建人名称
                    .append("CRTE_OPTINS_NO,")//创建机构编号
                    .append("OPTER_ID,")//操作员
                    .append("OPTER_NAME,")//操作员名称
                    .append("OPT_TIME,")//操作时间
                    .append("OPTINS_NO)")//操作员编号
                    .append("value ('");
            stringBuffer.append(data.getFixmedinsCode()).append(split); //机构编码
            stringBuffer.append(data.getFixmedinsName()).append(split); //机构名称
//            stringBuffer.append("07:30").append(split); //营业开始时间
//            stringBuffer.append("21:15").append(split); //营业结束时间
            stringBuffer.append(data.getLnt()).append(split); //经度
            stringBuffer.append(data.getLat()).append(split); //纬度
            stringBuffer.append(data.getAddr()).append(split);//地址
//            stringBuffer.append("1").append(split);//是否支持配送
//            stringBuffer.append("5").append(split);//支持配送公里数
            stringBuffer.append("0").append(split);//有效标志
            stringBuffer.append("pmc").append(split);//创建人
            stringBuffer.append("pmc").append(split);//创建人名称
            stringBuffer.append(data.getFixmedinsCode()).append(split);//创建机构编号
            stringBuffer.append("pmc").append(split);//操作员
            stringBuffer.append("pmc").append(split);//操作员名称
            stringBuffer.append("2024-12-12 18:00:00").append(split);//操作时间
            stringBuffer.append("pmc").append(end);//操作员编号
            String sql = stringBuffer.toString();
            beasInfoList.add(sql);
            System.out.println(sql);
        }
        System.out.println("#-----------------------云药店基本信息表---------------------------");
        return beasInfoList;
    }

    private static void invokeSendPlugIn (List<ImportPlaceData> list) {
        for (ImportPlaceData data : list) {
            sendPlugIn(data.getFixmedinsCode());
        }
    }

    private static void sendPlugIn(String orgId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("plugsToken","202307BBe17ace7f056a46fc947161d8ba350X31");
        jsonObject.put("env","prod");
        jsonObject.put("appId","2021003197616869");
        jsonObject.put("orgId", orgId);
        try {
            String url = "https://pub.ylbzj.yhwch.com/ampminiapp/org/plugs/token/saveOrUpdate";
            String param = JSONObject.toJSONString(jsonObject);
            String result = HttpUtil.doPostJson(url, param);

            System.out.println(orgId + "===添加插件，返回结果：{" + result + "}");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private static void yaodianBieMing(List<ImportPlaceData> list) {
        for (ImportPlaceData data : list) {
          String changeBieMing = "update hibiz_db.place_cloud_rtal_phac_b set VALI_FLAG = '1',PLACE_CLOUD_PHAC_NAME = '"
                  + data.getAlias() + "' where FIXMEDINS_CODE = '" + data.getFixmedinsCode() + "';";
            System.out.println(changeBieMing);
        }
    }
}
